package com.weipai.springboot.module.controller;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Apk;
import com.weipai.springboot.model.CBase;
import com.weipai.springboot.model.Picture;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.CommonService;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.util.PushUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: gusl
 * @Date: 2019/7/22 10:25
 * @Description:
 */
@RestController
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    FileUploadService fileUploadService;
    @Autowired
    private CommonService commonService;


    /**
     * 上传文件
     * @param picture
     * @return
     */
    @PostMapping("/common/saveFile")
    public Response saveBase(HttpServletRequest request, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveBaseUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                response.setCode(RetCode.SUCCESS);
                response.setMsg(RetCode.SUCCESS.getMsg());
                response.setData(result);
            }
        }else{
            response.setCode(RetCode.IMG_NULL_ERROR);
            response.setMsg(RetCode.IMG_NULL_ERROR.getMsg());
        }
        return response;
    }


    /**
     *  基本设置添加
     */
    @ResponseBody
    @RequestMapping(value = "/common/saveCbaer",method = RequestMethod.POST )
    public Response saveCbaer( CBase cBase){
        return  commonService.saveCbaer(cBase);
    }

    /**
     *  基本设置添加
     */
    @ResponseBody
    @RequestMapping(value = "/common/searchCbaer")
    public Response searchCbaer(){
        return  commonService.searchCbaer();
    }

    /**
     *  营销推送
     */
    @ResponseBody
    @RequestMapping(value = "/common/sendPush")
    public Response sendPush(String title,String Content){
        Response response = new Response();
        try {
            PushUtil.sendAndroidAndIOS(null,"broadcast", title, Content);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("营销推送异常:"+e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
            response.setMsg(RetCode.REQUEST_ERROR.getMsg());
            return  response;
        }
        response.setCode(RetCode.SUCCESS);
        response.setMsg(RetCode.SUCCESS.getMsg());
        return  response;
    }

}
